<template>
  <div class="container">
    <a-space direction="vertical" style="width: 100%" :size="16">
      <a-card style="width: 100%" title="心愿单排行">
        <a-form :model="formState">
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-item label="商品名称">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.searchCondition"
                  placeholder="输入商品名称关键词"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="商品编号">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.sku"
                  placeholder="输入商品编号"
                />
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="所属品牌">
                <a-input
                  style="width: 100%"
                  v-model:value="formState.brandName"
                  placeholder="输入所属品牌"
                />
              </a-form-item>
            </a-col>

            <a-col :span="6">
              <a-form-item label="商品类目">
                <a-select
                  ref="select"
                  v-model:value="formState.typeId"
                  style="width: 100%"
                  placeholder="商品类目"
                  @change="handleChangeType"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <a-select-option
                    v-for="item in goodsTypeList"
                    :key="item.id"
                    :value="item.id"
                    >{{ item.name }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="子类目">
                <a-select
                  ref="select"
                  v-model:value="formState.subTypeId"
                  style="width: 100%"
                  placeholder="商品子类目"
                >
                  <a-select-option :value="0">全部</a-select-option>
                  <template
                    v-if="
                      subcategoryList[0] &&
                      subcategoryList[0].childrenList.length > 0
                    "
                  >
                    <a-select-option
                      v-for="item in subcategoryList[0].childrenList"
                      :key="item.id"
                      :value="item.id"
                      >{{ item.name }}</a-select-option
                    >
                  </template>
                </a-select>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>

        <div class="layout-flexSpace">
          <a-space wrap>
            <a-button type="primary" @click="handleSearch">查询</a-button>
            <a-button @click="handleReset">重置</a-button>
          </a-space>
          <a-space wrap>
            <a-button type="primary" @click="handleExport">导出</a-button>
          </a-space>
        </div>
      </a-card>

      <a-card>
        <a-table
          :dataSource="dataSource"
          :columns="rankColumns"
          :pagination="pagination"
          @change="handleChangeSize"
        >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'name'">
            <div style="display: flex; align-items: center">
              <a-image
                :src="record.coverPic"
                :preview="{
                  src: record.coverPic,
                }"
                :width="60"
              />
              <div style="margin-left: 20px">
                <div>{{ record.name }}</div>
              </div>
            </div>
          </template>
        </template>
        </a-table>
      </a-card>
    </a-space>
  </div>
</template>
<script setup>
import { ref, onMounted } from "vue";
import { rankColumns } from "./columns.js";
import { goodsType } from "@/api/order";
import { collectRankingList, exportCollectRanking } from "@/api/goods";
import { exporeFile } from "@/utils/exporeFile";
const pagination = ref({
  current: 1,
  pageSize: 10,
  total: 0,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total) => `共 ${total} 条`,
});
const formState = ref({
  page: pagination.value.current,
  pageSize: pagination.value.pageSize,
  searchCondition: undefined,
  brandName: undefined,
  sku: undefined,
  subTypeId: undefined,
  typeId: undefined,
});
const dataSource = ref([]);

const goodsTypeList = ref([]);
const getGoodsType = async () => {
  const result = await goodsType();
  if (result.code == 200) {
    goodsTypeList.value = result.data;
  }
};
const subcategoryList = ref([]);
function handleChangeType(val) {
  formState.value.subTypeId = undefined;
  subcategoryList.value = goodsTypeList.value.filter(
    (value) => value.id == val
  );
}
function handleSearch() {
  formState.value.page = 1
  formState.value.pageSize = 10
  getCollectRankingList();
}
function handleReset() {
  formState.value = {
    page: 1,
    pageSize: 10,
    categoryId: undefined,
    subCategoryId: undefined,
    searchCondition: undefined,
    status: undefined,
  };
  pagination.value.current = 1
  pagination.value.pageSize = 10
  getCollectRankingList();
}
const getCollectRankingList = async () => {
  const result = await collectRankingList(formState.value);
  if (result.code == 200) {
    dataSource.value = result.data.list;
    pagination.value.total = result.data.total;
  }
};
const handleExport = async () => {
  const data = await exportCollectRanking(formState.value);
  exporeFile("心愿单排行列表.xls", data);
}
function handleChangeSize(page) {
  formState.value.page = page.current;
  formState.value.pageSize = page.pageSize;
  pagination.value.current = page.current;
  pagination.value.pageSize = page.pageSize;
  getCollectRankingList();
}
onMounted(() => {
  getCollectRankingList();
  getGoodsType();
});
</script>

<style lang="less">
.img-item {
  display: flex;
  flex-direction: column;
  align-items: center;

  .img-label {
    margin-top: 6px;
    color: #9599a5;
    font-size: 12px;
  }
}
</style>
